package practise;

import javax.inject.Named;

import practise.APIResponse.Status;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.googlecode.objectify.ObjectifyService;

import static com.googlecode.objectify.ObjectifyService.ofy;


@Api(name = "sizetest")
public class SizeTestApi {
	
	static {
		ObjectifyService.factory().register(EmailListEntity.class);
	}
	
	@ApiMethod(httpMethod = HttpMethod.PUT, path="testsize")
	public APIResponse putEmailListEntity(@Named("numMembers") long numMembers) {
		
		EmailListEntity entity = new EmailListEntity();
		
		for (long i = 0; i < numMembers; i++) {
			entity.members.add(i);
			entity.members.add(i);
		}
		
		
		long startTime = System.nanoTime();
		ofy().save().entity(entity).now();
		long timeElapsed = (System.nanoTime() - startTime) / 1000 / 1000;
		
		APIResponse response = new APIResponse();
		response.statusCode = Status.SUCCESS;
		//response.object = entity;
		response.userFriendlyMessage = "No of members (from size) = [" + entity.members.size() + 
				"], time elapsed in milliseconds = [" + timeElapsed + "], id = [" + entity.id + "]";
		
		return response;
	}
	
	@ApiMethod(httpMethod = HttpMethod.GET, path="gettestsize")
	public APIResponse getEmailListEntity(@Named("id") long id, @Named("cache") boolean cache) {
		
		long startTime = System.nanoTime();
		EmailListEntity entity = ofy().cache(cache).load().type(EmailListEntity.class).id(id).get();
		long timeElapsed = (System.nanoTime() - startTime) / 1000 / 1000;
								
		APIResponse response = new APIResponse();
		response.statusCode = Status.SUCCESS;
		//response.object = entity;
		response.userFriendlyMessage = "No of members (from size) = [" + entity.members.size() + 
				"], time elapsed in milliseconds = [" + timeElapsed + 
				"], cache = [" + cache + "], id = [" + entity.id + "]";
		
		return response;
	}
}
